const Actions = require('./actions');
const DB = require('app/db.js');
const monStorage = require('clientConfig/util/StoreData').monStorage;
const Storage = require('clientConfig/util/StoreData').Storage;
import { Control } from 'react-keeper'

import { NEW, RUNNING, TERMINATED, COMPLETED, CANCELED } from 'clientConfig/constants'
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        listData: []
    },

    onFetchList: function (pageNum, props, success, error) {
        let t = this;
        if (Control.state.origin.split('/')[0] == "workplan" || Control.state.origin.split('/')[0] == "tender") {
            DB.Approval.get_approval_list({
                PageNum: pageNum,
                CountForAPage: props.pageSize,
                module_type: Control.state.origin.split('/')[0],
                status:COMPLETED,
                proj_category:Control.state.origin.split('/')[0] == "workplan" ? "software" : "tenderplan" ,
                proj_name: props.proj_name ? props.proj_name : "" ,
                proj_num: props.proj_num ? props.proj_num : "",
                id:Storage.get('user.id') 
            }).then(function (content) {
                var list = [];
                var obj = {};
                for (let key in content.list) {
                    obj["badge"] = false;
                    obj["title"] = content.list[key].proj_name;
                    obj["process_id"] = content.list[key].process_id;
                    obj["content"] = [
                        { "unitcontent": content.list[key].proj_num },
                    ];
                    list.push(obj);
                    obj = {};
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if(props.proj_name || props.proj_num)
                    t.updateComponent()
                if(success)
                    success(hasNoMore, list);
            }).catch(function (error) { });
        } else {
            //#WARNING
            DB.Proj.get_payment_overview({ 
                proj_category: 'payplan',
                proj_name: props.proj_name ? props.proj_name : "" ,
                proj_num: props.proj_num ? props.proj_num : "" ,
                // status:RUNNING|COMPLETED|TERMINATED,
                id: monStorage.get('user.id')
            }).then(function (content) {
                var list = [];
                content.list.map((item, index) => {
                    var obj = {
                        "badge": false,
                        "title": item.proj_name,
                        "process_id": item.process_id,
                        "content": [
                            { "unitcontent": item.proj_num }
                        ]
                    }
                    list.push(obj);
                });
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if(props.proj_name || props.proj_num)
                    t.updateComponent()
                if(success)
                    success(hasNoMore, list);
            }).catch(function (error) { });
        }
    },
    updateComponent: function () {
        this.trigger(this.data);
    },
});
